#include<stdio.h>
#include<stdlib.h>

struct POINT
{
	long long stu_num;
	char name[21];
	int x;
	int y;
	int z;
};

int down1(const void *a,const void *b);
int down2(const void *a,const void *b);
int down3(const void *a,const void *b);
int main(void)
{
	int n,choose;
	scanf("%d",&n);
	struct POINT p[n];
	for(int i=0;i<n;i++)
	{
		scanf("%lld %s %d%d%d",&p[i].stu_num,p[i].name,&p[i].x,&p[i].y,&p[i].z);
	}
	scanf("%d",&choose);
	
	if(choose==1)
	{
		qsort(p,n,sizeof(struct POINT),down1);
	}
	else if(choose==2)
	{
		qsort(p,n,sizeof(struct POINT),down2);
	}
	else
	{
		qsort(p,n,sizeof(struct POINT),down3);
	}
	
	for(int i=0;i<n;i++)
	{
		printf("%lld %s %d %d %d\n",p[i].stu_num,p[i].name,p[i].x,p[i].y,p[i].z);
	}
	return 0;
}
int down1(const void *a,const void *b)
{
	struct POINT *s1=(struct POINT *)a;
	struct POINT *s2=(struct POINT *)b;
	return s2->x-s1->x;
}
int down2(const void *a,const void *b)
{
	struct POINT *s1=(struct POINT *)a;
	struct POINT *s2=(struct POINT *)b;
	return s2->y-s1->y;
}
int down3(const void *a,const void *b)
{
	struct POINT *s1=(struct POINT *)a;
	struct POINT *s2=(struct POINT *)b;
	return s2->z-s1->z;
}
